package presentation.windowsFackData;

import presentation.windows.PanelObserver;
import presentation.windows.SharedUIData;
import java.awt.Dimension;
import java.awt.Font;
import java.rmi.RemoteException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import util.Log;
import util.NotFoundException;
import util.Subsystem;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import domain.ForumLogic;



/**
 * for fake data panels
 */
public class BasePanelFake extends JPanel {

	private static final long serialVersionUID = 7928544302484474295L ;



	private JLabel loggedInLable;
	private SharedUIData sharedUIData;

	/** the observer of changes in this panel */
	private PanelObserver observer;

	/** the sub-frames that were created by this panel and are still open */
	private Vector<JFrame> openSubFrames;

	/** forum copy from the main panel (local or via server) **/
	protected ForumLogic forum;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}



	/**
	 * constructor.
	 * @param sharedData the shared data of all UI frames and panels
	 */
	public BasePanelFake(final SharedUIData sharedData) {
		super();
		this.forum = MainPanelFake.getForum();
		this.sharedUIData = sharedData;
		this.observer = null;
		this.openSubFrames = new Vector<JFrame>();
		this.initGUI();
	}

	/**
	 * @param subFrame open sub-frame to add to this panel
	 */
	public void addOpenSubFrame(final JFrame subFrame)
	{
		this.openSubFrames.add(subFrame);
	}

	/**
	 * attach the observer to observe this panel (there is only one observer at a time)
	 * @param obs - the observer to attach
	 */
	public void attachObserver(final PanelObserver obs)
	{
		this.observer = obs;
	}

	/**
	 * dispose all open sub-frames and remove them from the openSubFrames vector
	 */
	public void closeOpenSubFrames(){
		for(int i=0; i<this.openSubFrames.size(); i++){
			this.openSubFrames.elementAt(i).dispose();
		}
		this.openSubFrames = new Vector<JFrame>();
	}

	/**
	 * detach the current observer, so no one will observe this panel
	 */
	public void detachObserver()
	{
		this.observer = null;
	}

	/**
	 * @return the shared data of all panels
	 */
	public SharedUIData getSharedData()
	{
		return this.sharedUIData;
	}

	/**
	 * make this internal frame invisible
	 */
	public void makeInvisible()
	{
		this.closeOpenSubFrames();
		this.setVisible(false);
	}

	/**
	 * make this internal frame visible, and refresh it.
	 */
	public void makeVisible()
	{
		//refresh logged-in indicator icon:
		if(this.sharedUIData.isLoggedIn())
		{
			long userId = this.sharedUIData.getUser().getId();
			String userName = "unknown";
			try
			{
				userName = (this.forum.getMemberById(userId)).getUsername();
			}
			catch(NotFoundException e)
			{
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+userId+" could not found.") ;
			}
			catch (RemoteException e)
			{
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("communication problem when trying to get username.") ;
			}
			this.loggedInLable.setText("logged in as "+userName);
		}
		else
		{
			this.loggedInLable.setText("guest");
		}
		//make this frame visible:
		this.setVisible(true);
	}

	/**
	 * notify the observer for completing the performance of this panel,
	 * and this panel can be closed.
	 */
	public void notifyCompleted()
	{
		this.observer.updateActivePanel();
	}

	/**
	 * @param subFrame open sub-frame to remove (because it was closed)
	 */
	public void removeOpenSubFrame(final JFrame subFrame)
	{
		this.openSubFrames.remove(subFrame);
	}

	private void initGUI() {

		AnchorLayout thisLayout = new AnchorLayout();
		this.setLayout(thisLayout);
		this.setPreferredSize(new Dimension(735, 566));
		this.loggedInLable = new JLabel();
		this.add(this.loggedInLable, new AnchorConstraint(22, 422, 73, 114, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
		this.loggedInLable.setFont(new Font("Francais",1,16));
		this.loggedInLable.setPreferredSize(new java.awt.Dimension(226, 29));

		//refresh logged-in indicator icon:
		if(this.sharedUIData.isLoggedIn())
		{
			long userId = this.sharedUIData.getUser().getId();
			String userName = "unknown";
			try
			{
				userName = (this.forum.getMemberById(userId)).getUsername();
			}
			catch(NotFoundException e)
			{
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+userId+" could not found.") ;
			}
			catch (RemoteException e)
			{
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("communication problem when trying to get username.") ;
			}
			this.loggedInLable.setText("logged in as "+userName);
		}
		else
		{
			this.loggedInLable.setText("guest");
		}
		//make this frame visible:
		this.setVisible(true);
	}


}
